package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.entity.User;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 根据时间段统计营业数据
     * @param begin
     * @param end
     * @return
     */
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */

//        Map map = new HashMap();
//        map.put("begin",begin);
//        map.put("end",end);

        //查询总订单数
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("order_time",begin);
        queryWrapper.le("order_time",end);
        Integer totalOrderCount = Math.toIntExact(ordersMapper.selectCount(queryWrapper));

//        map.put("status", Orders.COMPLETED);
        //营业额
        queryWrapper.eq("status", Orders.COMPLETED);

        Double turnover = 0.0;
        List<Orders> ordersList = ordersMapper.selectList(queryWrapper);
        for(Orders order : ordersList) {
            Double s = order.getAmount().doubleValue();
            turnover += s;
        }

        //有效订单数
        Integer validOrderCount = Math.toIntExact(ordersMapper.selectCount(queryWrapper));

        Double unitPrice = 0.0;

        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0 && validOrderCount != 0){
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover / validOrderCount;
        }
        unitPrice = Math.round(unitPrice * 100) * 0.01d;

        //新增用户数
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.ge("create_time",begin);
        userQueryWrapper.le("create_time",end);
        Integer newUsers = Math.toIntExact(userMapper.selectCount(userQueryWrapper));

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }


    /**
     * 查询订单管理数据
     *
     * @return
     */
    public OrderOverViewVO getOrderOverView() {
        Map map = new HashMap();
        map.put("begin", LocalDateTime.now().with(LocalTime.MIN));
        map.put("status", Orders.TO_BE_CONFIRMED);

        //待接单
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("order_time", LocalDateTime.now().with(LocalTime.MIN));
        queryWrapper.eq("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = Math.toIntExact(ordersMapper.selectCount(queryWrapper));

        //待派送
        map.put("status", Orders.CONFIRMED);
        queryWrapper.clear();
        queryWrapper.ge("order_time", LocalDateTime.now().with(LocalTime.MIN));
        queryWrapper.eq("status", Orders.CONFIRMED);
        Integer deliveredOrders = Math.toIntExact(ordersMapper.selectCount(queryWrapper));

        //已完成
        queryWrapper.clear();
        queryWrapper.ge("order_time", LocalDateTime.now().with(LocalTime.MIN));
        queryWrapper.eq("status", Orders.COMPLETED);
        Integer completedOrders = Math.toIntExact(ordersMapper.selectCount(queryWrapper));

        //已取消
        queryWrapper.clear();
        queryWrapper.ge("order_time", LocalDateTime.now().with(LocalTime.MIN));
        queryWrapper.eq("status", Orders.CANCELLED);
        Integer cancelledOrders = Math.toIntExact(ordersMapper.selectCount(queryWrapper));

        //全部订单
        queryWrapper.clear();
        queryWrapper.ge("order_time", LocalDateTime.now().with(LocalTime.MIN));
        Integer allOrders = Math.toIntExact(ordersMapper.selectCount(queryWrapper));

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    public DishOverViewVO getDishOverView() {
//        Map map = new HashMap();
//        map.put("status", StatusConstant.ENABLE);
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", StatusConstant.ENABLE);
        Integer sold = Math.toIntExact(dishMapper.selectCount(queryWrapper));

        queryWrapper.clear();
        queryWrapper.eq("status", StatusConstant.DISABLE);
//        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = Math.toIntExact(dishMapper.selectCount(queryWrapper));

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    public SetmealOverViewVO getSetmealOverView() {
//        Map map = new HashMap();
//        map.put("status", StatusConstant.ENABLE);
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", StatusConstant.ENABLE);
        Integer sold = Math.toIntExact(setmealMapper.selectCount(queryWrapper));

//        map.put("status", StatusConstant.DISABLE);
        queryWrapper.clear();
        queryWrapper.eq("status", StatusConstant.DISABLE);
        Integer discontinued = Math.toIntExact(setmealMapper.selectCount(queryWrapper));

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
